//
//  OpinionViewController.m
//  DaDaBao
//
//  Created by user on 15/8/19.
//  Copyright (c) 2015年 User. All rights reserved.
//
#import "OpinionViewController.h"

@interface OpinionViewController ()<UITextFieldDelegate,UITextViewDelegate>
@property (nonatomic,strong) UITextView  * opinionView;
@property (nonatomic,strong) UITextField * contactTextField;//联系方式
@property (nonatomic,strong) UITextField * nameTextField;//名字
@property (nonatomic,copy)   NSString    * textViewPlaceString;//实现placeholder效果，记录上一次的string
@property (nonatomic,copy)   NSString    * nameTextPlaceString;
@property (nonatomic,copy)   NSString    * contactPlaceString;
@property (nonatomic,strong) UIScrollView * backScrollView;
@end

@implementation OpinionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    
    
    _backScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _backScrollView.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    [self.view addSubview:_backScrollView];
    
    [self createTheNavigationBar];
    
    _opinionView = [[UITextView alloc]initWithFrame:CGRectMake(10,64.0f + 10, SCREEN_WIDTH - 20, 150)];
    _opinionView.delegate = self;
    _opinionView.layer.cornerRadius = 5;
    _opinionView.layer.borderWidth = 0.3;
    _opinionView.text = @"您有什么意见或建议，可以在这里填写（60字以内）";
    _opinionView.textColor = RGBA(194, 194, 200, 1);
    _opinionView.font = [UIFont systemFontOfSize:17];
    _opinionView.backgroundColor = [UIColor whiteColor];
    _opinionView.layer.borderColor = [[UIColor grayColor]CGColor];
    [_backScrollView addSubview:_opinionView];
    
    
    _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(10,64.0f + 170, SCREEN_WIDTH - 20, 40)];
    _nameTextField.text = @"请写下您的姓名";
    _nameTextField.textColor = RGBA(194, 194, 200, 1);
    _nameTextField.delegate = self;
    [_nameTextField addTarget:self action:@selector(textfielddidChanged:) forControlEvents:UIControlEventEditingChanged];
    _nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [_backScrollView addSubview:_nameTextField];
    
    _contactTextField = [[UITextField alloc]initWithFrame:CGRectMake(10,64.0f + 220, SCREEN_WIDTH - 20, 40)];
    _contactTextField.text = @"请留下您的手机号码或者邮箱";
    _contactTextField.textColor = RGBA(194, 194, 200, 1);
    _contactTextField.delegate = self;
    [_contactTextField addTarget:self action:@selector(textfielddidChanged:) forControlEvents:UIControlEventEditingChanged];
    _contactTextField.borderStyle = UITextBorderStyleRoundedRect;
    [_backScrollView addSubview:_contactTextField];
    
    UIButton * commitBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 64.0f + 290, SCREEN_WIDTH - 20, 35.0f*HSCALE)];
    [commitBtn setTitle:@"提 交" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    commitBtn.backgroundColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    [commitBtn addTarget:self action:@selector(buttonCommitTapped:) forControlEvents:UIControlEventTouchUpInside];
    commitBtn.layer.cornerRadius = 5;
    [_backScrollView addSubview:commitBtn];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification  object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)keyboardWillHide:(NSNotification *)note
{
    _backScrollView.contentSize = CGSizeMake(0.0f, 0.0f);
}

#pragma mark ------------创建导航栏
-(void)createTheNavigationBar
{
    [[UIApplication sharedApplication]setStatusBarHidden:FALSE];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    topView.backgroundColor = [UIColor hexStringToColor:COLOR_VALUE];
    [self.view addSubview:topView];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, 20.0f, SCREEN_WIDTH, 44.0f)];
    titleLab.backgroundColor = [UIColor clearColor];
    titleLab.font = [UIFont boldSystemFontOfSize:20.0f];
    titleLab.textColor = [UIColor whiteColor];
    titleLab.text = @"意见反馈";
    titleLab.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLab];
    
    //返回按钮
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, 20.0f, 64.0f, 44.0f)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
}
#pragma mark-返回方法
-(void)buttonBackTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:TRUE];
}
#pragma mark------提交按钮绑定方法
-(void)buttonCommitTapped:(id)sender
{
    if (_opinionView.text.length<=0||[_opinionView.text isEqualToString:@"您有什么意见或建议，可以在这里填写（60字以内）"])
    {
        ALERT_VIEW(@"请填写意见");
        return;
    }
    if (_nameTextField.text.length<=0||[_nameTextField.text isEqualToString:@"请写下您的姓名"])
    {
        ALERT_VIEW(@"请填写姓名");
        return;
    }
    if (_contactTextField.text.length <=0||[_contactTextField.text isEqualToString:@"请留下您的手机号码或者邮箱"])
    {
        ALERT_VIEW(@"请填写联系方式");
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:TRUE];
    
    [AppGlobalClass PostDataWithNSstring:OPINION_URL withDictionary:@{@"mobile":_contactTextField.text,@"content":_opinionView.text,@"name":_nameTextField.text} success:^(NSDictionary *dictionary) {
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            NSString * str = [dictionary objectForKey:@"message"];
            ALERT_VIEW(str);
            [self.navigationController popViewControllerAnimated:TRUE];
        }else
        {
            NSString * str = [dictionary objectForKey:@"message"];
            ALERT_VIEW(str);
        }
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
    } networkerror:^(NSString *networkerror) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
    }];
}
#pragma mark--------------textViewDelegate
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.text=_textViewPlaceString;
    textView.textColor=[UIColor blackColor];
    _backScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+216);
}
-(void)textViewDidChange:(UITextView *)textView
{
    NSString *toBeString = textView.text;
    NSString * lang=[[[UIApplication sharedApplication]textInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 60) {
                textView.text = [toBeString substringToIndex:60];
            }
        }
    }
    else{
        if (toBeString.length > 60) {
            textView.text = [toBeString substringToIndex:60];
        }
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    _textViewPlaceString=textView.text;
    textView.textColor=[UIColor blackColor];
    if (textView.text.length<=0)
    {
        textView.text=@"您有什么意见或建议，可以在这里填写（60字以内）";
        textView.textColor=[UIColor colorWithRed:194.0/255 green:194.0/255 blue:200.0/255 alpha:1];
    }
  
}
#pragma mark----------------textFieldDelegate
-(void)textfielddidChanged:(UITextField *)textField
{
    NSString *toBeString = textField.text;
    NSString * lang=[[[UIApplication sharedApplication]textInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 20) {
                textField.text = [toBeString substringToIndex:20];
            }
        }
    }
    else{
        if (toBeString.length > 20) {
            textField.text = [toBeString substringToIndex:20];
        }
    }
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (_nameTextField == textField)
    {
        textField.text=_nameTextPlaceString;
    }else
    {
        textField.text=_contactPlaceString;
    }
     textField.textColor=[UIColor blackColor];
    _backScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+216);
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (_nameTextField == textField)
    {
        _nameTextPlaceString=textField.text;
        textField.textColor=[UIColor blackColor];
        if (textField.text.length<=0)
        {
            textField.text=@"请写下您的姓名";
            textField.textColor=[UIColor colorWithRed:194.0/255 green:194.0/255 blue:200.0/255 alpha:1];
        }
    }else
    {
        _contactPlaceString=textField.text;
        textField.textColor=[UIColor blackColor];
        if (textField.text.length<=0)
        {
            textField.text=@"请留下您的手机号码或者邮箱";
            textField.textColor=[UIColor colorWithRed:194.0/255 green:194.0/255 blue:200.0/255 alpha:1];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:TRUE];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
